package ti.ml.car;

import algo.Node;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.StyledEditorKit;
import ti.ml.car.domain.Controller;
import ti.ml.car.domain.Direction;

/**
 *
 * @author robin
 */
public class Route implements Runnable {

    private List<Direction> routeList;
    private Car car;
    private Controller c;

    @SuppressWarnings("empty-statement")
    public Route(Car car, List<Node> path) {
        this.car = car;
        c = Controller.getIntance();
        routeList = new ArrayList<Direction>();
        int previousPos = car.getPos();

        for (int i = 1; i < path.size(); i++) {
            int block = path.get(i).block;
            if (previousPos - 20 == block) {
                routeList.add(Direction.up);
            } else if (previousPos + 20 == block) {
                routeList.add(Direction.down);
            } else if (previousPos + 1 == block) {
                routeList.add(Direction.right);
            } else if (previousPos - 1 == block) {
                routeList.add(Direction.left);
            } else {
                c.appendTxtLog("ongeldige beweging: van: " + previousPos + " naar: " + block);
            }
            previousPos = block;
        }
    }

    public void followRoute() {
        for (Direction d : routeList) {
            try {
                switch (d) {
                    case left:
                        car.moveLeft();
                        Thread.sleep(1000);

                        break;
                    case right:
                        car.moveRight();
                        Thread.sleep(1000);
                        c.appendTxtLog("Moving right");
                        break;
                    case up:
                        car.moveUp();
                        Thread.sleep(1000);
                        c.appendTxtLog("Moving up");
                        break;
                    case down:
                        car.moveDown();
                        Thread.sleep(1000);
                        c.appendTxtLog("Moving down");

                        break;
                }
            } catch (Exception e) {
                c.appendTxtLog("" + e.getMessage());
            }
        }
        int pathLength=routeList.size();
        c.endOfRoute("End of route reached.\nRoute length was "+pathLength+" squares.");
    }

    @Override
    public void run() {
        for (Direction d : routeList) {
            try {
                switch (d) {
                    case left:
                        c.appendTxtLog("Moving left");
                        car.moveLeft();
                        Thread.sleep(500);

                        break;
                    case right:
                        c.appendTxtLog("Moving right");
                        car.moveRight();
                        Thread.sleep(500);

                        break;
                    case up:
                        c.appendTxtLog("Moving up");
                        car.moveUp();
                        Thread.sleep(500);

                        break;
                    case down:
                        c.appendTxtLog("Moving down");
                        car.moveDown();
                        Thread.sleep(500);


                        break;
                }
            } catch (Exception e) {
                c.appendTxtLog("" + e.getMessage());
            }
        }
        
        int pathLength=routeList.size();
        c.endOfRoute("End of route reached.\nRoute length was "+pathLength+" squares.");
    }
}
